home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / misc / Radio / radio / ourutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-14  |  7.7 KB  |  396 lines

  1. #include "gopher.h"
  2.  
  3. #ifdef SYSVCURSES
  4. #include <termio.h>
  5.  
  6. #define       STDOUT  1
  7.  
  8.  
  9. /* this is some modified elm source  (Raw) */
  10. static int inraw = 0;
  11.  
  12. struct termio raw_tty,
  13.            original_tty;
  14.  
  15. #define OFF 0
  16. #define ON 1
  17. #define    ttgetattr(fd,where)    ioctl((fd),TCGETA,(where))
  18. #define    ttsetattr(fd,where)    ioctl((fd),TCSETAW,(where))
  19.  
  20.  
  21. Raw(state)
  22. int state;
  23. {
  24.      /** state is either ON or OFF, as indicated by call **/
  25.      
  26.      if (state == OFF && inraw) {
  27.           (void) ttsetattr(STDOUT,&original_tty);
  28.           inraw = 0;
  29.      }
  30.      else if (state == ON && ! inraw) {
  31.       
  32.           (void) ttgetattr(STDOUT, &original_tty);
  33.           (void) ttgetattr(STDOUT, &raw_tty);    /** again! **/
  34.       
  35.           raw_tty.c_lflag &= ~(ICANON | ECHO); /* noecho raw mode        */
  36.       
  37.           raw_tty.c_cc[VMIN] = '\01';  /* minimum # of chars to queue    */
  38.           raw_tty.c_cc[VTIME] = '\0';  /* minimum time to wait for input */
  39.       
  40.           (void) ttsetattr(STDOUT, &raw_tty);
  41.           inraw = 1;
  42.      }
  43. }
  44.  
  45. #endif
  46.  
  47.  
  48. outchar(c)
  49. char c;
  50. {
  51.         /** output the given character.  From tputs... **/
  52.         /** Note: this CANNOT be a macro!              **/
  53.  
  54.         putc(c, stdout);
  55. }
  56.  
  57.  
  58.  
  59. /*
  60. ** This procedure exits out of the curses environment and
  61. ** displays the file indicated by pathname to the screen
  62. ** using a pager command of some sort 
  63. */
  64.  
  65. void
  66. display_file(Filename)
  67.   char *Filename;
  68. {
  69.      FILE *tmpfile, *fNew;
  70.      char command[MAXSTR];
  71.      static char SaveName[MAXSTR];
  72.      char ch;
  73.  
  74.      strcpy(command, PagerCommand);
  75.  
  76.      exit_curses();
  77.  
  78.      /** Execute the PAGER command **/
  79.      strcat(command, " ");
  80.      strcat(command, Filename);
  81.  
  82.      
  83.      if (strcmp(PagerCommand, "builtin") != 0)
  84.       system(command);
  85.      else
  86.       Ourpager(Filename);
  87.  
  88.      printf("Press <SPACE> to continue, <s> to save, or <p> to print:");
  89.      fflush(stdout);
  90.      noecho();
  91.      cbreak();
  92.  
  93.      /** System V systems don't want to go back into cbreak mode, so we
  94.        force it into cbreak, canonical, etc mode. ***/
  95.  
  96. #ifdef SYSVCURSES
  97.      Raw(ON);
  98. #endif
  99.  
  100.      ch = 0;
  101.      while (ch == 0) {
  102.       ch=getchar();
  103.  
  104.       switch(ch) {
  105.       case '\n':
  106.       case '\r':
  107.       case ' ' :
  108.            break;
  109.       case 's':
  110.            sprintf(command, "Enter save file name: ");
  111.            GetOneOption(command, SaveName);
  112.            if (strlen(SaveName) == 0)
  113.             break;
  114.            while ((fNew = fopen(SaveName, "wt")) == NULL) {
  115.             bzero(SaveName, MAXSTR);
  116.             sprintf(command, "Error opening %s:  Enter new name or <Enter> to cancel: ", SaveName);
  117.             GetOneOption(command, SaveName);
  118.             if (SaveName[0] == 0)
  119.              break;
  120.            }
  121.               
  122.            if (fNew == NULL)
  123.             break;
  124.  
  125.            if ((tmpfile = fopen(Filename, "rt")) == NULL)
  126.             fprintf(stderr, "%s cannot be opened.\n", Filename), exit(tmpfile);
  127.            while (!feof(tmpfile)) fputc(fgetc(tmpfile), fNew);
  128.  
  129.            fclose(tmpfile);
  130.            fclose(fNew);
  131.  
  132.            break;
  133.       case 'p':
  134.            sprintf(command, "%s %s\n", PRINTER_COMMAND, Filename);
  135.            system(command);
  136.            break;
  137.       default:
  138.            printf("%s",sGAudibleBell);
  139.            fflush(stdout);
  140.            ch=0;
  141.            break;
  142.       }
  143.      }
  144.      
  145.      tputs(sGClearscreen,1,outchar);
  146.      fflush(stdout);
  147.  
  148. #ifdef SYSVCURSES
  149.      Raw(OFF);
  150. #endif
  151. }
  152.  
  153.  
  154. /*
  155. ** This mini pager is intended for people worried about shell escapes from
  156. ** more or less or whatever
  157. */
  158.  
  159.  
  160. Ourpager(filename)
  161.   char *filename;
  162. {
  163.      FILE *InFile;
  164.      int i;
  165.      char inputline[512], *cp;
  166.      int Done = FALSE;
  167.      char ZeTypedChar;
  168.      
  169.      if ((InFile = fopen(filename, "r")) == NULL)
  170.       return;
  171.  
  172.      while (Done == FALSE) {
  173.       tputs(sGClearscreen,1,outchar);
  174.  
  175.       for (i=0 ; i < LINES; i++) {
  176.            cp = fgets(inputline, 512, InFile);
  177.            ZapCRLF(inputline);
  178.            puts(inputline);
  179.       }
  180.  
  181.       printf("----Press <SPACE> for next page, q to exit------");
  182.       
  183. #ifdef SYSVCURSES
  184.       Raw(ON);
  185. #else
  186.       cbreak();
  187. #endif
  188.  
  189.       ZeTypedChar = getchar();
  190.       
  191.       if ((ZeTypedChar == 'q') || (cp == NULL)) {
  192.            printf("\n");
  193.            Done = TRUE;
  194.       }
  195.      }
  196.  
  197. #ifdef SYSVCURSES
  198.      Raw(OFF);
  199. #else
  200.      cbreak();
  201. #endif
  202.  
  203.      fclose(InFile);
  204. }
  205. /*
  206. ** Non System V getstr's all seem to be broken in some way.  Anyways
  207. ** a normal getstr wouldn't do for us.  This one displays a current value
  208. ** That the use can edit.
  209. **
  210. */
  211.  
  212.  
  213. Mygetstr(inputline)
  214.   char *inputline;
  215. {
  216.      int pointer = 0;
  217.      char ch;
  218.  
  219.      cbreak();
  220.      noecho();     
  221.  
  222.      /*** Check to see if there's something in the inputline already ***/
  223.      
  224.      while (inputline[pointer] != '\0') {
  225.       addch(inputline[pointer]);
  226.       pointer ++;
  227.      }
  228.  
  229.      refresh();
  230.       
  231.  
  232.      for (;;) {
  233.       ch = getch();
  234.  
  235.       switch (ch) {
  236.  
  237.       case '\n':
  238.       case '\r':
  239.       case '\t':
  240.            inputline[pointer] = '\0';
  241.            return;
  242.            break;
  243.  
  244.       case '\010':
  245.       case '\177':
  246.            if (pointer > 0) {
  247.             addch('\010');
  248.             delch();
  249.            
  250.             inputline[--pointer] = '\0';
  251.             refresh();
  252.            }
  253.            break;
  254.             
  255.       default:
  256.            inputline[pointer++]= ch;
  257.            addch(ch);
  258.            refresh();
  259.       }
  260.      }
  261. }
  262.  
  263. /*
  264. ** This routine will allow the user to change a whole bunch of fields.
  265. ** 
  266. ** The maximum number of options is hard set at 9 right now.  It may be
  267. ** different in the future.
  268. **
  269. ** The space for storing stuff is provided by the caller.  This routine
  270. ** will present it to the user in this fashion:
  271. **
  272. **           Option1 : responses1
  273. **           Option2 : responses2
  274. **           .....
  275. **
  276. ** It would be wise to keep the length of the options and responses
  277. ** below 38 characters.
  278. **
  279. */
  280.  
  281.  
  282. Get_Options(Title, Err, numOptions, Options, Responses)
  283.   char *Title;
  284.   char Err[MAXSTR];
  285.   int    numOptions;
  286.   char **Options;
  287.   char Responses[MAXRESP][MAXSTR];
  288. {
  289.      int         availlines;
  290.      static char printstring[80];
  291.      static char inputline[80];
  292.      int         optionlen;
  293.      int         maxoptionlen;
  294.      int         i,j;          /** Acme Buggy whips and integers **/
  295.      BOOLEAN     Done = FALSE;
  296.      char        ch;
  297.  
  298.      while (Done == FALSE) {
  299.  
  300.       clear();
  301.       Draw_Banner();
  302.       Draw_Status("Press <RETURN> to exit");
  303.       Centerline( Title, 3);
  304.       Centerline( Err, 4);
  305.       
  306.       availlines = LINES - 6;
  307.       
  308.       /** Find the longest width of the options strings **/
  309.       
  310.       maxoptionlen = 0;
  311.       
  312.       for (i=0; i<numOptions; i++) {
  313.            j= strlen(Options[i]);
  314.            maxoptionlen = (maxoptionlen > j) ? maxoptionlen:j;
  315.       }
  316.       
  317.       
  318.       /*** Print out the options in a nice looking fashion ***/
  319.       
  320.       for (i=0; i<numOptions; i++) {
  321.            optionlen = strlen(Options[i]);
  322.            mvaddch(i+5, 2, ('0' + 1 + i));
  323.            addstr(". ");
  324.            addstr(Options[i]);
  325.            mvaddstr(i+5, maxoptionlen + 6, ": ");
  326.            addstr(Responses[i]);
  327.       }
  328.  
  329.       Centerline("Select an Option to Change:", LINES-3);
  330.  
  331.       refresh();
  332.  
  333.       /*** Now get some user input ***/
  334.  
  335.       ch = getch();
  336.  
  337.  
  338.       if (ch == '\n' || ch == '\r' || ch == 'Q' || ch=='q')
  339.            Done = TRUE;
  340.       
  341.       else if (isdigit(ch)) {
  342.            i = ch - '1' ;
  343.            Draw_Status("Press Return when finished");
  344.            move( i +5, maxoptionlen +8);
  345.            clrtoeol();
  346.            refresh();
  347.  
  348.            echo();
  349.            inputline[0] = '\0';
  350.            Mygetstr(Responses[i]);
  351.            noecho();
  352.  
  353.            /*strcpy(Responses[i], inputline);*/
  354.  
  355.       }
  356.       else
  357.            addstr(sGAudibleBell);
  358.      }
  359.            
  360. }
  361.  
  362.  
  363. GetOneOption(OptionName, Response)
  364.   char *OptionName, *Response;
  365. {
  366.      mvaddstr(LINES-1, 0, OptionName);
  367.      clrtoeol();
  368.      refresh();
  369.      echo();
  370.      Mygetstr(Response);
  371.      noecho();
  372. }
  373.  
  374. /*
  375.  ** This strips off  CR's and LF's leaving us with a nice pure string.
  376.  */
  377.  
  378. void
  379. ZapCRLF(inputline)
  380. char *inputline;
  381. {
  382.      char *cp;
  383.      
  384.      cp = index(inputline, '\r');    /* Zap CR-LF */
  385.      if (cp != NULL)
  386.           *cp = '\0';
  387.      else {
  388.           cp = index(inputline, '\n');
  389.           if (cp != NULL)
  390.                *cp = '\0';
  391.      }
  392. }
  393.  
  394.  
  395.  
  396.